/**
 *
 * node {
 *    name:     //标签名
 *    attrs:    //属性集合
 *    children: //子元素
 *    data:     //附加数据
 *    script:   //脚本语言
 * };
 *
 */




var dragData = {};

(function(){
    dragData.create = function(container){
        container = $(container);
        if(!container.length)
            return;
        var result = [];
        container.each(function(){
            var data = {
                name: this.nodeName,
                attr: {},
                // children: []
            };
            var attrs = this.attributes;
            Object.keys(attrs).forEach(function(key){
                var attr = attrs[key];
                data.attr[attr.name] = attr.value;
            });
            data.children = dragData.create($(this).children());
            result.push(data);
        });
        return result;
    };

    // dragData.addChildren = function(node, children){
    //     if(!node || !children || children.length === 0)
    //         return null;
    //
    //     if(!children.length)
    //         children = [children];
    //
    //     children.forEach(function(v){
    //         node.children.push(dragData.create(v));
    //     });
    // };

    dragData.parse = function(data){
        if(!data)
            return;
        if(typeof data === "string")
            data = eval("(" + data + ")");
        if(data.length == null)
            data = [data];

        var result = [];
        data.forEach(function(v){
            var el = $(document.createElement(v.name));
            if(v.attr){
                el.attr(v.attr);
            }
            if(v.children)
                el.append(dragData.parse(v.children));

            result.push(el[0]);
        });
        return $(result);
    };
})();
